package org.higginsexamples.multiusermail.rcpmail;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.eclipse.higgins.rp.icard.token.identity.DigitalIdentity;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class MailboxClient {

	// attributes

	private final String emailAddress;
	private final String password;
	private final DigitalIdentity informationCard;

	// constructors

	public MailboxClient(String _emailAddress, String _password,
			DigitalIdentity _icard) {
		this.emailAddress = _emailAddress;
		this.password = _password;
		this.informationCard = _icard;

	}

	// public methods

	public RcpMailModel load() throws Exception {

		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod("http://localhost:8099/mailboxes");
		// HttpMethod method = new
		// GetMethod("http://frankgerhardt.com/tmp/model.xml");

		int resultCode;

		while (true) {
			resultCode = client.executeMethod(method);

			switch (resultCode) {
			case 200:
				client.getState().setCredentials(AuthScope.ANY, null);
				return handleResponse(method);

			case 401:
				if (handleLogin(client, method)) {
					break;
				}

			default:
				throw new HttpException("Unexpected response code: "
						+ resultCode);
			}
		}
	}

	protected boolean handleLogin(HttpClient client, HttpMethod method) {
		XmlsoapAttributesCredentials creds = new XmlsoapAttributesCredentials();
		boolean mocked = true;
		if (mocked) {

			// Mock implementation - to be removed when iCard is plugged-in
			// creds.setAttribute( SecurityConstants.XMLSOAP_EMAIL,
			// "fred.flintstone@stoneage.com,tom@tomandjerry.org" ) ;
			// creds.setAttribute( SecurityConstants.XMLSOAP_FIRSTNAME, "Fred" )
			// ;
			// creds.setAttribute( SecurityConstants.XMLSOAP_LASTNAME,
			// "Flintstone" ) ;
			creds.setAttribute(SecurityConstants.XMLSOAP_EMAIL,
					"jochen.hiller@gmx.net");
			creds.setAttribute(SecurityConstants.XMLSOAP_FIRSTNAME, "Jochen");
			creds.setAttribute(SecurityConstants.XMLSOAP_LASTNAME, "Hiller");

			client.getState().setCredentials(AuthScope.ANY, creds);
		} else {
			creds.setAttribute(SecurityConstants.XMLSOAP_EMAIL,
					"jochen.hiller@gmx.net");
			creds.setAttribute(SecurityConstants.XMLSOAP_FIRSTNAME, "Jochen");
			creds.setAttribute(SecurityConstants.XMLSOAP_LASTNAME, "Hiller");

			client.getState().setCredentials(AuthScope.ANY, creds);
		}

		return true;
	}

	protected RcpMailModel handleResponse(HttpMethod method) throws IOException {
		InputStream response = method.getResponseBodyAsStream();

		XStream xstream = new XStream(new DomDriver());
		xstream.alias("mailbox", Mailbox.class);
		xstream.alias("folder", Folder.class);
		xstream.alias("rcpmail", RcpMailModel.class);

		return (RcpMailModel) xstream.fromXML(response);
	}

}